
var currentPage = 0
/*
currentSearch == -1 权限全部搜索
currentSearch == -2 权限条件搜索/
 */
var currentSearch = -1
$(document).ready(function () {
    addTimeOutAction()

    getAllPermissionsInformation()

    /*
    搜索添加回车绑定事件/
     */
    $('#search-permissionsName').on('keypress', function (event) {
        if(event.keyCode == '13'){
            searchPermissionByNameLike()
        }
    })
})

/*
获取全部权限信息/
 */
function getAllPermissionsInformation(page_ = 0) {
    currentSearch = -1
    currentPage = page_
    var size = 10
    var sortFieldName = 'id'
    var asc = 1
    var urlStr = ipPort + '/permission/getAllByPage?page='+ currentPage + '&size=' + size + '&sortFieldName=' + sortFieldName + '&asc=' + asc
    $.ajax({
        url:urlStr,
        dataType:'json',
        success:function (obj) {
            setPermissionTableInformation(obj)
        },
        error:function (error) {
            console.log(error)
        }
    })
}
/*
通过名称搜索权限信息/
 */
function searchPermissionByNameLike(page_ = 0) {
    var name = $('#search-permissionsName').val()
    if(name == ''){
        alert("请输入名称！")
        return
    }
    currentSearch = -2
    currentPage = page_
    var size = 10
    var sortFieldName = 'id'
    var asc = 1

    var urlStr = ipPort + '/permission/getByNameLikeByPage?size='+ size + "&page=" + currentPage + "&sortFieldName=" + sortFieldName + "&asc=" + asc + "&name=" + name
    $.ajax({
        url:urlStr,
        dataType:'json',
        success:function (obj) {
            if(obj.code == 0){
                setPermissionTableInformation(obj)
            }else{
                alert(obj.message)
            }
        },
        error:function (error) {
            console.log(error)
        }
    })
}
/*
设置权限table信息/
 */
function setPermissionTableInformation(obj) {
    $('#allPermissionsInformationPanel .currentPage').text(currentPage + 1)
    $('#allPermissionsInformationPanel .totalPage').text(obj.data.totalPages)
    var table_tr = $('.table-tr')
    var permission_id = $('.permission-id')
    var permission_name = $('.permission-name')
    for(var i = 0; i < obj.data.numberOfElements; i++){
        table_tr.eq(i).removeClass('hidden')
        permission_id.eq(i).text(obj.data.content[i].id)
        permission_name.eq(i).text(obj.data.content[i].name)
    }
    for (var i = obj.data.numberOfElements; i < 10; i++){
        table_tr.eq(i).addClass('hidden')
    }
}

/*
通过权限id查询角色 并设置查看角色模块/
 */
function setRoleModalInformation(thisObj) {
    var permissionId = $(thisObj).parent().parent().find('.permission-id').text()
    $.ajax({
        url:ipPort + '/permission/getByRolesByPermissionId?permissionId=' + permissionId,
        dataType:'json',
        success:function (obj) {
            var table_trr = $('.table-trr')
            var permissions_roleId = $('.permissions-roleId')
            var permissions_roleName = $('.permissions-roleName')
            var permissions_roleDescribe = $('.permissions-roleDescribe')
            for(var  i = 0; i < obj.data.length; i++){
                table_trr.eq(i).removeClass('hidden')
                permissions_roleId.eq(i).text(obj.data[i].id)
                permissions_roleName.eq(i).text(obj.data[i].name)
                permissions_roleDescribe.eq(i).text(obj.data[i].description)
            }
            for (var i = obj.data.length; i < table_trr.length; i++){
                table_trr.eq(i).addClass('hidden')
            }
        },
        error:function (error) {
            console.log(error)
        }
    })
}

/*************************************选取多个角色modal*****************************************/
/*
获取所有角色/
 */
function getAllRole_multi(thisObj) {
    $('.selectedRole-button').attr('val', $(thisObj).parent().parent().find('.permission-id').text())
    $('.selectRole-role-ul li').remove()
    $('.selectedRole-role-ul li').remove()
    $.ajax({
        url:ipPort + '/role/getAll',
        dataType:'json',
        success:function (obj) {
            if(obj.data.length != 0){
                for(var j = 0; j < obj.data.length; j++){
                    var ul = $('.selectRole-role-ul')
                    var appendStr = '<li onclick="selectedRole(this)"><img src="imgs/tips_department_up.png" height="20px" style="margin-top: -2px;margin-right: 5px"><span ' + 'value="' + obj.data[j].id + '">' + obj.data[j].name + '</span></li>'
                    ul.append(appendStr)
                }
            }
        },
        error:function (error) {
            console.log(error)
        }
    })
    var permissionId = $(thisObj).parent().parent().find('.permission-id').text()
    $.ajax({
        url:ipPort + '/permission/getByRolesByPermissionId?permissionId=' + permissionId,
        dataType:'json',
        success:function (obj) {
            var selectedStaffUl = $('#form-selectRole .selectedRole-role-ul')
            for(var  i = 0; i < obj.data.length; i++){
                var appendStr = '<li><img src="imgs/mine.png" height="20px" style="margin-top: -2px"><span class="selectedRole-span" ' + 'value="' + obj.data[i].id + '">' + obj.data[i].name + '</span><span class="cancel-span" onclick="cancelSelectRole(this)" aria-hidden="true" style="display: block; float: right">&times;</span></li>'
                selectedStaffUl.append(appendStr)
            }
        },
        error:function (error) {
            console.log(error)
        }
    })
}
/*
选区人员/
 */
function selectedRole(thisObj) {
    var selectedStaffUl = $('#form-selectRole .selectedRole-role-ul')
    var appendStr = '<li><img src="imgs/mine.png" height="20px" style="margin-top: -2px"><span class="selectedRole-span" ' + 'value="' + $(thisObj).find("span").attr("value") + '">' + $(thisObj).find("span").text() + '</span><span class="cancel-span" onclick="cancelSelectRole(this)" aria-hidden="true" style="display: block; float: right">&times;</span></li>'
    selectedStaffUl.append(appendStr)
}
/*
取消选区/
 */
function cancelSelectRole(thisObj) {
    $(thisObj).parent().remove()
}
/*
选定人员/
 */
function selectedAndAddRole() {
    var selectedStaff_span = $('.selectedRole-span')
    var roleId = []
    for(var i = 0; i < selectedStaff_span.length; i++){
        roleId.push(parseInt(selectedStaff_span.eq(i).attr('value')))
    }
    var permissionsId = $('.selectedRole-button').attr('val')

    $.ajax({
        url:ipPort + '/permission/assignRolesToPermission?permissionId=' + permissionsId + "&roleIds=" + roleId,
        dataType:'json',
        type: 'post',
        success:function (obj) {
            if(obj.code == 0){
                alert("设置成功")
            }
            else{
                alert(obj.message)
            }
        },
        error:function (error) {
            alert(obj.message)
        }
    })
}
/******************************************************************************/


/*
上一页/
 */
function previousPage(str) {
    var currentPage_ = $(str).find('.currentPage').text()
    if(currentPage_ == 1){
        alert("已经是第一页！")
        return
    }
    currentPage--
    if(currentPage < 0){
        currentPage = 0
    }
    if(currentSearch == -1){
        getAllPermissionsInformation(currentPage)
    }else  if(currentSearch == -2){
        searchPermissionByNameLike(currentPage)
    }

}
/*
下一页/
 */
function nextPage(str) {
    var currentPage_ = $(str).find('.currentPage').text()
    var totalPage_ = $(str).find('.totalPage').text()
    if(currentPage_ == totalPage_){
        alert("已经是最后一页！")
        return
    }
    currentPage++
    if(currentSearch == -1){
        getAllPermissionsInformation(currentPage)
    }else  if(currentSearch == -2){
        searchPermissionByNameLike(currentPage)
    }




}
/*
跳转页/
 */
function skipPage(str) {
    var skipPage_ = parseInt( $(str).find('.skipPage').val())
    var totalPage_ = parseInt( $(str).find('.totalPage').text())
    if(skipPage_ - totalPage_ > 0){
        alert("没有此页！")
        return
    }
    if(skipPage_ < 1){
        alert("没有此页！")
        return
    }
    currentPage = skipPage_ - 1
    if(currentSearch == -1){
        getAllPermissionsInformation(currentPage)
    }else  if(currentSearch == -2){
        searchPermissionByNameLike(currentPage)
    }
}